clear all
set more off, perm
set mem 10000000
set matsize 10000
version 13

****************************************************************************** 
*** Table and figures for JPE revisions: first-stage nightttime brightness ***
****************************************************************************** 

** Set file paths
do "$path_code/paths.do"

** Set graph scheme
cd "$path/code/analyze"
set scheme fb, perm

********************************************************************************
********************************************************************************

** Figure 5: Nighttime brightness RD coefficients by year
{
use "${results}/RDROBUST_fs_lights.dta", clear
keep if fs_step==1
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
assert kernel=="Triangular"
assert polynomial==1
assert fals==""
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
sum bw_lo
local bw_lo = string(r(min),"%9.0f")
local bw_hi = string(r(max),"%9.0f")

twoway (rspike uci_robust lci_robust yvar, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar, msize(medsmall) mc(gs4) msymbol(O)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	///title("RD: Village-Level Brightness by Year", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.125 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	 legend(off) aspect(0.5)
cap erase "$texfig/rd_lights_coeffs_by_year-eps-converted-to.pdf"
graph export "$texfig/rd_lights_coeffs_by_year.eps", replace 



   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_lights_by_year.tex", write text replace
 
file write tablewrite "\begin{figure}[p]\centering" _n
file write tablewrite "\caption{Village-level RD estimates in nighttime brightness, by year}" _n
file write tablewrite "\label{fig:rd_lights_by_year}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.65\textwidth, trim={1mm 16mm 1mm 16mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year.eps} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- This figure plots RD coefficients for " _n
file write tablewrite "maximum nighttime brightness at the village level. We estimate a separate regression " _n
file write tablewrite "for each year, with \texttt{rdrobust} specifications identical to those in Table \ref{tab:rd_vdelec}. " _n
file write tablewrite "Each regression controls for pre-2005 brightness at the village level; " _n
file write tablewrite "2002--2005 regressions control for brightness in years preceding the outcome variable. " _n
file write tablewrite "Optimal bandwidths for these regressions range from `bw_lo' to `bw_hi'. " _n
file write tablewrite "Results are robust to alternative controls, kernels, bandwidth algorithms, and standard errors. " _n
file write tablewrite "See notes under Table \ref{tab:rd_vdelec} for details." _n
file write tablewrite "Appendix Tables \ref*{tab:rd_lights} and \ref*{tab:rd_lights_pre} report these results numerically. " _n
file write tablewrite "Whiskers display 95\% confidence intervals. " _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Figure 6: Nighttime brightness DD event study figure
{
	
cap erase "$results/dd figs/event_study_lights_10thplan-eps-converted-to.pdf"
cap erase "$results/dd figs/event_study_lights_bothplans-eps-converted-to.pdf"
copy "$results/dd figs/event_study_lights_10thplan.eps" "${texfig}/event_study_lights_10thplan.eps" , replace
copy "$results/dd figs/event_study_lights_bothplans.eps" "${texfig}/event_study_lights_bothplans.eps" , replace

   // MAKE FIGURE
file open tablewrite using "$texfig/figure_event_study_lights.tex", write text replace
 
file write tablewrite "\begin{figure}[p]\centering" _n
file write tablewrite "\caption{Village-level DD event studies in nighttime brightness}" _n
file write tablewrite "\label{fig:event_study_lights}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/event_study_lights_10thplan.eps} " _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/event_study_lights_bothplans.eps}" _n
file write tablewrite "\vspace*{-9mm}" _n
file write tablewrite "\caption*{\scriptsize Note. ---  Village-level DD event studies using annual nighttime brightness from 1998 to 2013. " _n 
file write tablewrite "The outcome variable is maximum brightness in each year, for each village polygon. " _n
file write tablewrite "In the left panel, treatment (RGGVY eligibility) turns on in the year when each 10th-Plan district first " _n
file write tablewrite "received RGGVY project funds, using 11th-Plan districts as controls. " _n
file write tablewrite "In the right panel, treatment turns on for both 10th- and 11th-Plan districts, in the first year the district " _n
file write tablewrite "received RGGVY funds. 10th-Plan districts first received funds in 2005--2007, while 11th-Plan districts " _n
file write tablewrite "first received funds in 2008--2011." _n 
file write tablewrite "In both panels, the omitted year is the last year prior to a district's first receipt of funds." _n
file write tablewrite "Regressions include village fixed effects, state-by-year fixed effects, and village-specific linear time trends." _n
file write tablewrite "Estimation samples include 10th- and 11th-Plan districts in states with reliable shapefiles, without restricting village size. " _n
file write tablewrite "Whiskers display 95\% confidence intervals, with standard errors clustered by Census block." _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Figure A1: Difference in discontinuities figures for nighttime brightness
{
copy "$results/dd figs/diff_in_disc_lights_dt_150.pdf" "${texfig}/diff_in_disc_lights_dt_150.pdf" , replace
copy "$results/dd figs/diff_in_disc_lights_dt_100.pdf" "${texfig}/diff_in_disc_lights_dt_100.pdf" , replace

   // MAKE FIGURE
file open tablewrite using "$texfig/figure_diff_in_disc_lights.tex", write text replace
 
file write tablewrite "\begin{figure}[h]\centering" _n
file write tablewrite "\caption{Difference in discontinuities -- village-level nighttime brightness}" _n
file write tablewrite "\label{fig:diff_in_disc_lights}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/diff_in_disc_lights_dt_150.pdf} " _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/diff_in_disc_lights_dt_100.pdf}" _n
file write tablewrite "\vspace*{-11mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Village-level RD coefficients by year, estimated jointly " _n 
file write tablewrite "via Equation (\ref{eq:diff_in_disc}). " _n
file write tablewrite "Yearly coefficients plot differential RD LATEs relative to the RD LATE of 2005 (the omitted year)." _n
file write tablewrite "Each regression controls for separate linear polynomials for each year (both above and below the cutoff), "  _n
file write tablewrite "village fixed effects, and district-by-year fixed effects. We weight observations by " _n
file write tablewrite "distance from the cutoff using a triangular kernel, which aligns with our preferred \texttt{rdrobust} specification.  " _n
file write tablewrite "The left panel imposes a common RD bandwidth of 150 above/below the 300-person cutoff for all years; " _n
file write tablewrite "the right panel narrows this bandwidth to 100. " _n
file write tablewrite "Our estimation sample includes within-bandwidth single-habitation villages in RGGVY 10th-Plan districts. " _n
file write tablewrite "Whiskers display 95\% confidence intervals, with standard errors clustered by Census block." _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Table A4: Pooled DD for nighttime brightness
{
use "${results}/dd_results_lights_allyears.dta", clear
keep if yvar=="lights_max"
keep if dd_model=="pooled"
keep if ifs==""
keep if fes=="pca01_id year exp05_st_4ile#c.year exp05_ntl_10ile#c.year"
assert _N==3

gen c = .
replace c = 1 if dd_var==1 
replace c = 2 if dd_var==2
replace c = 3 if dd_var==3
assert c!=.	
sort c

forvalues c = 1/3 {
		
	local beta`c' = string(beta[`c'],"%9.3f")
	local se`c' = string(se[`c'],"%9.3f")
	if pvalue[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pvalue[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pvalue[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	local nobs`c' = string(nobs[`c']/1e6,"%9.2f") + "M"
	local ymean`c' = string(ymean[`c'],"%9.2f")
}	


   // MAKE TABLE
file open tablewrite using "$textab/table_dd_village_lights.tex", write text replace

file write tablewrite "\begin{table}[h!]\centering " _n
file write tablewrite "\caption{Village-level DD of nighttime brightness}" _n
file write tablewrite "\label{tab:dd_village_lights}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccc}" _n
file write tablewrite "\hline" _n
file write tablewrite "& \multicolumn{3}{c}{Outcome: Max village brightness} \\" _n
file write tablewrite "\vspace{-0.42cm}" _n
file write tablewrite "\\" _n
file write tablewrite " \cline{2-4} \\" _n
file write tablewrite "\vspace{-0.80cm}" _n
file write tablewrite " \\" _n
file write tablewrite "& ~1st wave\,\$\times\$\,2005~ & ~1st wave\,\$\times\$\,funds~ & ~Both waves\,\$\times\$\,funds~ \\" _n
file write tablewrite "& (1) & (2) & (3) \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "\$\mathbf{1}\big[\text{District is RGGVY-eligible}\big]\$ & \$`beta1'$`stars1' & $`beta2'$`stars2' & $`beta3'$`stars3' \\" _n
file write tablewrite "& ~~$(`se1')$~~ & ~~$(`se2')$~~ & ~~$(`se3')$~~  \\" _n
file write tablewrite "[0.8em]" _n
file write tablewrite "Mean of dep var & `ymean1' & `ymean2' & `ymean3' \\" _n
file write tablewrite "Village-year observations &`nobs1' & `nobs2' & `nobs3' \\" _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. ---  Village-level DD using annual nighttime brightness from 1998 to 2013. " _n
file write tablewrite "The outcome variable is maximum (raw) nighttime brightness in each year, across each village polygon. " _n
file write tablewrite "Column (1) defines treatment at the district-level, for 10th-Plan districts only, and turns on treatment in 2005. " _n
file write tablewrite "Column (2) staggers the timing of treatment based on when each 10th-Plan district received RGGVY project funds. " _n
file write tablewrite "Column (3) expands the definition of treatment to include 11th-Plan villages, with staggered treatment turning on the year " _n
file write tablewrite "each district received RGGVY project funds. " _n
file write tablewrite "All regressions include: district fixed effects; year fixed effects; " _n
file write tablewrite "linear trends in state quartiles of 2005 household expenditures per capita; " _n 
file write tablewrite "and linear trends in national deciles of 2005 household expenditures per capita. " _n
file write tablewrite "Estimation samples include villages in 10th-Plan districts, 11th-Plan districts, " _n
file write tablewrite "and non-RGGVY districts, without restricting village size, " _n
file write tablewrite "and in states with reliable village shapefiles." _n
file write tablewrite "Standard errors are clustered at the district level." _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite
}

********************************************************************************
********************************************************************************

** Figure A3: DD for nighttime brightness, by population bins
{
use "${results}/dd_results_lights_allyears.dta", clear
keep if yvar=="lights_max"
keep if dd_model=="pop bins"
keep if ifs==""
keep if fes=="pca01_id year#popbin exp05_st_4ile#c.year exp05_ntl_10ile#c.year"
assert _N==3

gen c = .
replace c = 1 if dd_var==1 
replace c = 2 if dd_var==2
replace c = 3 if dd_var==3
assert c!=.	
sort c

drop yvar-ifs beta-ci95_hi row_id
reshape long beta se tscore pvalue ci95_lo ci95_hi, i(ymean-c) j(pop) string
replace pop = subinstr(pop,"_"," ",2)
replace pop = word(pop,1)
destring pop, replace
replace pop = pop + 250
replace pop = pop - 50 if c==1
replace pop = pop + 50 if c==3

twoway ///
	(rspike ci95_hi ci95_lo pop if c==1, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(rspike ci95_hi ci95_lo pop if c==2, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(rspike ci95_hi ci95_lo pop if c==3, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta pop if c==1, msize(medsmall) mc(midblue) msymbol(O)) ///
	(scatter beta pop if c==2, msize(medsmall) mlc(navy) mfc(white) msymbol(T)) ///
	(scatter beta pop if c==3, msize(medsmall) mc(eltblue) msymbol(S)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("Binned DD coefficient (95% CI)") xtitle("Village population bin") ///
	///title("RD: Village-Level Brightness by Year", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(0(0.2).8,nogrid angle(0)) ///yscale(range(-0.125 0.4)) ///
	xlabel(0 500 1000 1500 2000 2500 3000 "3000+", labsize(medsmall)) ///
	legend(order(4 "1st-wave {&lowast} 2005   " 5 "1st-wave {&lowast} funds   " ///
				 6 "Both waves {&lowast} funds") c(3) pos(6)  size(medsmall)) ///	
	aspect(0.35)
graph export "$texfig/dd_lights_by_popbin.pdf", replace 



   // MAKE FIGURE
file open tablewrite using "$texfig/figure_dd_lights_by_popbin.tex", write text replace
 
file write tablewrite "\begin{figure}[h!]\centering" _n
file write tablewrite "\caption{Village-level DD of nighttime brightness, by village population bin}" _n
file write tablewrite "\label{fig:dd_lights_by_popbin}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.61\textwidth, trim={0 17mm 0 15mm}, clip]{${texfig_short}/dd_lights_by_popbin.pdf} " _n
file write tablewrite "\vspace{-5mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- This figure plots binned DD coefficients " _n
file write tablewrite "for maximum nighttime brightness." _n
file write tablewrite "We estimate three DD regressions analogous to those in Table \ref{tab:dd_village_lights}. " _n
file write tablewrite "Each regression interacts the DD coefficient with 500-person bins (as in Figure \ref{fig:dd_vdelec_by_popbin}). " _n
file write tablewrite "We interact year fixed effects with population bins, and also include village fixed effects " _n
file write tablewrite "and both sets of 2005 expenditure trends. " _n
file write tablewrite "Whiskers display 95\% confidence intervals, with standard errors clustered by district. " _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Table A10: Nighttime brightness RD numerical results, 2007-12
{
cap file close tablewrite
use "$results/RDROBUST_fs_lights.dta", clear
keep if fs_step==1
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
assert kernel=="Triangular"
assert polynomial==1
assert fals==""
drop if inlist(yvar,"lights_max2002_hat","lights_max2003_hat","lights_max2004_hat","lights_max2005_hat","lights_max2006_hat")

gen c = .
replace c = 1 if yvar=="lights_max2007_hat"
replace c = 2 if yvar=="lights_max2008_hat" 
replace c = 3 if yvar=="lights_max2009_hat"
replace c = 4 if yvar=="lights_max2010_hat"
replace c = 5 if yvar=="lights_max2011_hat"
replace c = 6 if yvar=="lights_max2012_hat"
assert c!=.	
sort c

forvalues c = 1/6 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	
	
   // MAKE TABLE
file open tablewrite using "$textab/table_rd_lights.tex", write text replace
 
file write tablewrite "\begin{table}[p]\centering" _n
file write tablewrite "\caption{Village-level RD in nighttime brightness}" _n
file write tablewrite "\label{tab:rd_lights}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccc} " _n
file write tablewrite "\hline " _n
file write tablewrite "& \multicolumn{6}{c}{Outcome: Maximum village brightness by year} \\ " _n
file write tablewrite "\vspace{-0.40cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "\cline{2-7} \\ " _n
file write tablewrite "\vspace{-0.80cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "& 2007 & 2008 & 2009 & 2010 & 2011 & 2012 \\ " _n
file write tablewrite "& (1) & (2) & (3) & (4) & (5) & (6)  \\ " _n
file write tablewrite "\hline " _n
file write tablewrite "$\mathbf{1}\big[\text{2001 pop \$\ge 300\$}\big]$ & " _n
file write tablewrite "$`beta1'$`stars1' & $`beta2'$`stars2' & $`beta3'$`stars3' & $`beta4'$`stars4' & $`beta5'$`stars5' & $`beta6'$`stars6' \\" _n
file write tablewrite "& $(`se1')$ & $(`se2')$ & $(`se3')$ & $(`se4')$ & $(`se5')$ & $(`se6')$ \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "Mean brightness (\$<300\$) & `ymean1' & `ymean2' & `ymean3' & `ymean4' & `ymean5' & `ymean6'  \\ " _n
file write tablewrite "Optimal bandwidth & `bw1' & `bw2' & `bw3' & `bw4' & `bw5' & `bw6' \\ " _n
file write tablewrite "Village observations &`nobs1' & `nobs2' & `nobs3' & `nobs4' & `nobs5' & `nobs6' \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- \texttt{rdrobust} specifications are identical to those in " _n
file write tablewrite "Table \ref*{tab:rd_vdelec}, and regressions are analogous to those in Figure \ref*{fig:rd_lights_by_year}. " _n
file write tablewrite "Results are robust to alternative controls, kernels, bandwidth algorithms, and standard errors. " _n
file write tablewrite "See notes under Figure \ref*{fig:rd_lights_by_year} for details." _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Figure A5: Nighttime brightness RD plots, 2007-2012 
{
forvalues y = 2007/2012 {
	copy "${results}/RDROBUST plots fs lights/lights_fs_preferred_`y'_wide_10.pdf" ///
		"${texfig}/lights_fs_preferred_`y'_wide_10.pdf" , replace
}

   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_lights_plots.tex", write text replace
 
file write tablewrite "\begin{figure}[p]\centering" _n
file write tablewrite "\caption{Village-level RDs in nighttime brightness, by year}" _n
file write tablewrite "\label{fig:rd_lights_plots}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/lights_fs_preferred_2007_wide_10.pdf}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/lights_fs_preferred_2008_wide_10.pdf}\\" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/lights_fs_preferred_2009_wide_10.pdf}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/lights_fs_preferred_2010_wide_10.pdf}\\" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/lights_fs_preferred_2011_wide_10.pdf}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/lights_fs_preferred_2012_wide_10.pdf}" _n
file write tablewrite "\vspace*{-9mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- RD plots for nighttime brightness by year, " _n
file write tablewrite "which correspond to Figure \ref*{fig:rd_lights_by_year} and Table \ref{tab:rd_lights}. " _n
file write tablewrite "We standardize the RD bandwidths and vertical axes " _n
file write tablewrite "of these brightness plots to facilitate visual comparisons across years. " _n
file write tablewrite "See notes under Figure \ref*{fig:rd_lights_by_year} for details." _n
file write tablewrite "Figure \ref{fig:rd_lights_plots_pre} displays analogous RD plots for 2002--06 nighttime brightness." _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Figure A7: Nighttime brightness RD by year, high vs. low implementation intensity
{
use "${results}/RDROBUST_fs_lights.dta", clear
append using "${results}/RDROBUST_fs_lights_dpr.dta"
keep if fs_step==1
assert ifs=="pop_mismatch20==0 & lights_diff<20"
keep if dprtag=="" | dprtag=="hi_vill" | dprtag=="lo_vill"
assert fe=="STFE*"
assert kernel=="Triangular"
assert polynomial==1

replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace

replace yvar = yvar - 0.20 if dprtag==""
replace yvar = yvar + 0.20 if dprtag=="lo_vill"

twoway ///
	(rspike uci_robust lci_robust yvar if dprtag=="", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(rspike uci_robust lci_robust yvar if dprtag=="hi_vill", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(rspike uci_robust lci_robust yvar if dprtag=="lo_vill", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if dprtag=="", msize(medsmall) mc(midblue) msymbol(O)) ///
	(scatter beta_robust yvar if dprtag=="hi_vill", msize(medsmall) mlc(navy) mfc(white) msymbol(0)) ///
	(scatter beta_robust yvar if dprtag=="lo_vill", msize(medsmall) mlc(maroon) mfc(white) msymbol(T)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)", size(small)) xtitle("") ///
	///title("RD: Village-Level Brightness by Year", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.2(0.2)0.6,nogrid angle(0) labsize(small)) yscale(range(-0.125 0.4)) ///
	xlabel(2002(1)2012, labsize(small)) ///
	legend(order(4 "Main RD sample (130 districts) " ///
	             5 "RGGVY treated >60% of villages (90 districts) " ///
				 6 "RGGVY treated <60% of villages (35 districts)") ///
			c(1) pos(6) size(small)) aspect(0.4)

graph export "$texfig/rd_lights_coeffs_by_year_intens.pdf", replace 



   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_lights_by_year_intens.tex", write text replace
 
file write tablewrite "\begin{figure}[h!]\centering" _n
file write tablewrite "\caption{Village-level RD in nighttime brightness, by RGGVY implementation intensity}" _n
file write tablewrite "\label{fig:rd_lights_by_year_intens}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.8\textwidth, trim={0 15mm 0 10mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_intens.pdf} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Solid blue markers reproduce our main " _n
file write tablewrite "nighttime brightness RD results from Figure \ref*{fig:rd_lights_by_year}. " _n
file write tablewrite "Hollow markers report RD results from identical regressions on two split samples: " _n
file write tablewrite "(i) the 90 districts where implementation data report that RGGVY treated over 60 " _n
file write tablewrite "percent of all villages in the district (navy circles); and (ii) 35 districts where " _n
file write tablewrite "RGGVY treated less than 60\% of district villages (maroon triangles). " _n
file write tablewrite "Split samples omit 5 10th-Plan districts with implausibly high counts of " _n
file write tablewrite "treated villages in RGGVY administrative data. See notes under Figure " _n
file write tablewrite "\ref*{fig:rd_lights_by_year} for further details. " _n
file write tablewrite "Results are robust to alternative kernels and bandwidth algorithms. " _n
file write tablewrite "Whiskers display 95\% confidence intervals. " _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Figure A9: Nighttime brightness RD by year, heterogeneous by hours/day of power supply
{
copy "$results/dd figs/diff_in_disc_lights_dt_150_hours.pdf" "${texfig}/diff_in_disc_lights_dt_150_hours.pdf" , replace
copy "$results/dd figs/diff_in_disc_lights_dt_100_hours.pdf" "${texfig}/diff_in_disc_lights_dt_100_hours.pdf" , replace


   // MAKE FIGURE
file open tablewrite using "$texfig/figure_diff_in_disc_lights_hours.tex", write text replace
 
file write tablewrite "\begin{figure}[p]\centering" _n
file write tablewrite "\caption{Difference in discontinuities -- village-level nighttime brightness \$\times\$ high/low power supply}" _n
file write tablewrite "\label{fig:diff_in_disc_lights_hours}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/diff_in_disc_lights_dt_150_hours.pdf} " _n
file write tablewrite "\includegraphics[width=0.495\textwidth]{${texfig_short}/diff_in_disc_lights_dt_100_hours.pdf}" _n
file write tablewrite "\vspace*{-9mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Difference-in-discontinuity estimates are identical to " _n 
file write tablewrite "those in \ref{fig:diff_in_disc_lights}, except that we interact the RD indicator with dummies for " _n
file write tablewrite "districts where rural villages receive over/under 10 hours per day of power supply. " _n
file write tablewrite "We calculate this variable by averaging hours of all-sector power (from the 2011 Census, at the village level) " _n
file write tablewrite "over all electrified villages in each district. " _n
file write tablewrite "Whiskers display 95\% confidence intervals, with standard errors clustered by Census block." _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Figure B2: Nighttime brightness RD by year, sensitivities
{
use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,8)
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
keep if polynomial==1
assert fals==""
unique yvar kernel
assert r(unique)==r(N)
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
replace yvar = yvar - 0.17 if kernel=="Triangular"
replace yvar = yvar + 0.17 if kernel=="Uniform"

gen lci10 = beta_robust - 1.645*se_robust
gen asterisk = .
replace asterisk = -0.05 if lci10>0 & lci_robust<0

twoway (rspike uci_robust lci_robust yvar if kernel=="Triangular", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if kernel=="Triangular", msize(medium) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust yvar if kernel=="Epanechnikov", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if kernel=="Epanechnikov", msize(medium) mlc(navy) mfc(white) msymbol(T)) ///
	(rspike uci_robust lci_robust yvar if kernel=="Uniform", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if kernel=="Uniform", msize(medium) mlc(eltblue) mfc(eltblue) msymbol(S)) ///
	(scatter asterisk yvar , msize(medium) mlc(gs10) mfc(white) msymbol(+)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	title("Sensitivity: Kernel in running variable", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.1 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	legend(order(2 "Triangular  " 4 "Epanechnikov  " 6 "Uniform") c(3) pos(6)) aspect(0.6)

graph export "$texfig/rd_lights_coeffs_by_year_sens_kernel.pdf", replace 

***

use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,7)
tab bwmethod if bw_lo==bw_hi
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
assert kernel=="Triangular"
keep if polynomial==1
keep if inlist(bwmethod,"mserd","msesum","cerrd")
assert fals==""
unique yvar bwmethod
assert r(unique)==r(N)
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
replace yvar = yvar - 0.17 if bwmethod=="mserd"
replace yvar = yvar + 0.17 if bwmethod=="cerrd"

gen lci10 = beta_robust - 1.645*se_robust
gen asterisk = .
replace asterisk = -0.05 if lci10>0 & lci_robust<0

twoway (rspike uci_robust lci_robust yvar if bwmethod=="mserd", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if bwmethod=="mserd", msize(medium) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust yvar if bwmethod=="msesum", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if bwmethod=="msesum", msize(medium) mlc(navy) mfc(white) msymbol(T)) ///
	(rspike uci_robust lci_robust yvar if bwmethod=="cerrd", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if bwmethod=="cerrd", msize(medium) mc(eltblue) msymbol(S)) ///
	(scatter asterisk yvar , msize(medium) mlc(gs10) mfc(white) msymbol(+)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	title("Sensitivity: Optimal bandwidth algorithm", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.1 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	legend(order(2 "MSE (difference)  " 4 "MSE (sum)  " 6 "CER (common)") c(3) pos(6)) aspect(0.6)

graph export "$texfig/rd_lights_coeffs_by_year_sens_bw.pdf", replace 

***

use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,12)
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
assert kernel=="Triangular"
assert bwmethod=="mserd"
assert polynomial==1
assert fals==""
keep if inlist(vce,"","vce(nncluster stdtbk)","vce(cluster stdtbk)","vce(cluster stdt)")
unique yvar vce
assert r(unique)==r(N)
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
replace yvar = yvar - 0.17*1.5 if vce==""
replace yvar = yvar - 0.17*0.5 if vce=="vce(nncluster stdtbk)"
replace yvar = yvar + 0.17*0.5 if vce=="vce(cluster stdtbk)"
replace yvar = yvar + 0.17*1.5 if vce=="vce(cluster stdt)"

gen lci10 = beta_robust - 1.645*se_robust
gen asterisk = .
replace asterisk = -0.05 if lci10>0 & lci_robust<0

twoway (rspike uci_robust lci_robust yvar if vce=="", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if vce=="", msize(medium) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust yvar if vce=="vce(nncluster stdtbk)", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if vce=="vce(nncluster stdtbk)", msize(medium) mlc(navy) mfc(white) msymbol(T)) ///
	(rspike uci_robust lci_robust yvar if vce=="vce(cluster stdtbk)", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if vce=="vce(cluster stdtbk)", msize(medium) mc(eltblue) msymbol(S)) ///
	(rspike uci_robust lci_robust yvar if vce=="vce(cluster stdt)", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if vce=="vce(cluster stdt)", msize(medium) mc(midblue) msymbol(X)) ///
	(scatter asterisk yvar , msize(medium) mlc(gs10) mfc(white) msymbol(+)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	title("Sensitivity: Standard errors", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.1 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	legend(order(2 "NN  " 4 "NN cluster by block  " 6 "Cluster by block  " 8 "Cluster by district") c(4) pos(6)) aspect(0.6)

graph export "$texfig/rd_lights_coeffs_by_year_sens_ses.pdf", replace 

***

use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,8,9)
assert ifs=="pop_mismatch20==0 & lights_diff<20"
keep if kernel=="Triangular"
assert bwmethod=="mserd"
assert polynomial==1 | fe=="STFE*"
assert fals==""
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
replace yvar = yvar - 0.17*1.5 if fe=="STFE*" & polynomial==1
replace yvar = yvar - 0.17*0.5 if fe=="" & polynomial==1
replace yvar = yvar + 0.17*0.5 if fe=="DTFE*" & polynomial==1
replace yvar = yvar + 0.17*1.5 if fe=="STFE*" & polynomial==2

gen lci10 = beta_robust - 1.645*se_robust
gen asterisk = .
replace asterisk = -0.05 if lci10>0 & lci_robust<0

twoway (rspike uci_robust lci_robust yvar if fe=="STFE*" & polynomial==1, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fe=="STFE*" & polynomial==1, msize(medium) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust yvar if fe=="" & polynomial==1, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fe=="" & polynomial==1, msize(medium) mlc(navy) mfc(white) msymbol(T)) ///
	(rspike uci_robust lci_robust yvar if fe=="DTFE*" & polynomial==1, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fe=="DTFE*" & polynomial==1, msize(medium) mc(eltblue) msymbol(S)) ///
	(rspike uci_robust lci_robust yvar if fe=="STFE*" & polynomial==2, lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fe=="STFE*" & polynomial==2, msize(medium) mc(midblue) msymbol(X)) ///
	(scatter asterisk yvar , msize(medium) mlc(gs10) mfc(white) msymbol(+)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	title("Sensitivity: Fixed effects and polynomial", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.1 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	legend(order(2 "State FEs  " 4 "No FEs  " 6 "District FEs  " 8 "2nd-order polynomial") c(4) pos(6)) aspect(0.6)

graph export "$texfig/rd_lights_coeffs_by_year_sens_fes.pdf", replace 

***

use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,5,6)
assert kernel=="Triangular"
assert bwmethod=="mserd"
assert polynomial==1
assert fals==""
keep if inlist(ifs,"pop_mismatch20==0 & lights_diff<20","lights_diff<20","pop_mismatch20==0 & lights_diff<50")
unique yvar ifs
assert r(unique)==r(N)
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
replace yvar = yvar - 0.17 if ifs=="pop_mismatch20==0 & lights_diff<20"
replace yvar = yvar + 0.17 if ifs=="pop_mismatch20==0 & lights_diff<50"

gen lci10 = beta_robust - 1.645*se_robust
gen asterisk = .
replace asterisk = -0.05 if lci10>0 & lci_robust<0

twoway (rspike uci_robust lci_robust yvar if ifs=="pop_mismatch20==0 & lights_diff<20", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if ifs=="pop_mismatch20==0 & lights_diff<20", msize(medium) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust yvar if ifs=="lights_diff<20", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if ifs=="lights_diff<20", msize(medium) mlc(navy) mfc(white) msymbol(T)) ///
	(rspike uci_robust lci_robust yvar if ifs=="pop_mismatch20==0 & lights_diff<50", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if ifs=="pop_mismatch20==0 & lights_diff<50", msize(medium) mc(eltblue) msymbol(S)) ///
	(scatter asterisk yvar , msize(medium) mlc(gs10) mfc(white) msymbol(+)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	title("Sensitivity: Including villages with inconsistent data", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.1 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	legend(order(2 "Preferred  " 4 "With pop mismatches  " 6 "With lights outliers") c(3) pos(6)) aspect(0.6)

graph export "$texfig/rd_lights_coeffs_by_year_sens_outliers.pdf", replace 

***

use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,2,3,4)
drop if !regexm(yvar,"2011") & fs_step==1
assert kernel=="Triangular"
assert bwmethod=="mserd"
assert polynomial==1
assert fals==""
gen x = .
replace x = 1 if yvar=="lights_max2011_hat"
replace x = 2 if yvar=="lights_max2011"
replace x = 3 if yvar=="lights_mean2011_hat"
replace x = 4 if yvar=="post_avg_max_10_12"
replace x = 5 if yvar=="post_avg_max_09_13"
replace x = 6 if yvar=="lights_sta_max2011_hat"
replace x = 7 if yvar=="lights_pct_max2011_hat"
drop if x==.

gen lci10 = beta_robust - 1.645*se_robust
gen asterisk = .
replace asterisk = -0.05 if lci10>0 & lci_robust<0

twoway ///
	(rspike uci_robust lci_robust x if x!=., lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust x if x==1, msize(medium) mc(midblue) msymbol(O))	///
	(scatter beta_robust x if x>1 & x!=., msize(medium) mlc(navy) mfc(white) msymbol(T)) ///
	(scatter asterisk x , msize(medium) mlc(gs10) mfc(white) msymbol(+)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	title("Sensitivity: Brightness variables, 2011 only", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.1 0.4)) ///
	xlabel(1(1)7) xscale(range(0.5 7.5)) ///
	xlabel( 1 `" "Max" "projected" "(preferred)" "'  ///
			2 `" "Max" "raw" "' ///
			3 `" "Mean" "projected" "' ///
			4 `" "Max" "3-year" "avg" "' ///
			5 `" "Max" "5-year" "avg" "' ///
			6 `" "Max" "projected" "(stable)" "' ///
			7 `" "Max" "projected" "(percent)" "' ///
			, labsize(medsmall)) ///
	legend(off) aspect(0.6)

graph export "$texfig/rd_lights_coeffs_by_year_sens_lightsvar.pdf", replace 




   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_lights_by_year_sens.tex", write text replace
 
file write tablewrite "\begin{figure}[p]\centering" _n
file write tablewrite "\caption{Sensitivities for village-level RD in nighttime brightness}" _n
file write tablewrite "\label{fig:rd_lights_by_year_sens}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "{\includegraphics[width=0.495\textwidth, trim={1mm 0mm 1mm 0mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_sens_kernel.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.495\textwidth, trim={1mm 0mm 1mm 0mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_sens_bw.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.495\textwidth, trim={1mm 0mm 1mm 0mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_sens_ses.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.495\textwidth, trim={1mm 0mm 1mm 0mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_sens_fes.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.495\textwidth, trim={1mm 0mm 1mm 0mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_sens_outliers.pdf}} " _n
file write tablewrite "{\includegraphics[width=0.495\textwidth, trim={1mm 0mm 1mm 0mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_sens_lightsvar.pdf}} " _n
file write tablewrite "\vspace*{-9mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- Each panel reports \texttt{rdrobust} point estimates and " _n
file write tablewrite " 95\% confidence intervals, for a separate set of sensitivity analyses. " _n
file write tablewrite "In all 6 panels, the solid blue circles report our preferred results from Figure \ref*{fig:rd_lights_by_year}." _n
file write tablewrite "The top-left panel compares our preferred triangular weighting kernel to the Epanechnikov and uniform kernels." _n
file write tablewrite "The top-right panel compares our preferred MSE-optimal bandwidth selection algorithm ({\tt mserd}) with " _n
file write tablewrite "the MSE-optimal bandwidth based on the sum ({\tt msesum}) rather than the difference, and with the " _n
file write tablewrite "coverage error rate optimal bandwidth algorithm ({\tt cerrd}). " _n
file write tablewrite "The middle-left panel applies alternative standard errors: nearest-neighbor clustering by Census block, " _n
file write tablewrite "clustering by Census block, and clustering by district. " _n
file write tablewrite "The middle-right panel removes state fixed effects, adds district fixed effects, and " _n
file write tablewrite "uses a 2nd-order polynomial in the running variable (with state fixed effects). " _n
file write tablewrite "The bottom-left panel alternately adds in (i) the 11\% of villages with population " _n
file write tablewrite "discrepancies in merged habitation data, and (ii) the 0.2\% of villages with " _n
file write tablewrite "extremely implausible 10-year changes in nighttime brightness. " _n
file write tablewrite "The bottom-right panel tests our preferred outcome variable (spatial maximum of NOAA's \`\`average'' nighttime " _n
file write tablewrite "brightness, linearly projected across adjacent years to remove measurement error) for sensitivity to using: " _n
file write tablewrite "raw brightness, spatial mean brightness, unweighted 3- and 5-year average brightness, and " _n
file write tablewrite "two alternative NOAA lights products  (more processed \`\`stable'' lights, and \`\`percent'' " _n
file write tablewrite "lights that weight by observed frequency)." _n
file write tablewrite "Pluses indicate significance at the 10\% level, but not at the 5\% level. " _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** Figure B4: Nighttime brightness RD by year, falsifications
{

use "${results}/RDROBUST_fs_lights.dta", clear
keep if inlist(fs_step,1,13)
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
assert kernel=="Triangular"
assert bwmethod=="mserd"
assert polynomial==1
replace yvar = subinstr(yvar,"lights_max","",.)
replace yvar = subinstr(yvar,"_hat","",.)
destring yvar, replace
replace yvar = yvar - 0.15*1.5 if fals==""
replace yvar = yvar - 0.15*0.5 if fals=="10th Plan, multi hab"
replace yvar = yvar + 0.15*0.5 if fals=="11th Plan, single hab"
replace yvar = yvar + 0.15*1.5 if fals=="11th Plan, multi hab"

twoway (rspike uci_robust lci_robust yvar if fals=="", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fals=="", msize(medsmall) mc(midblue) msymbol(O)) ///
	(rspike uci_robust lci_robust yvar if fals=="10th Plan, multi hab", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fals=="10th Plan, multi hab", msize(medsmall) mlc(maroon) mfc(white) msymbol(0)) ///
	(rspike uci_robust lci_robust yvar if fals=="11th Plan, single hab", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fals=="11th Plan, single hab", msize(medsmall) mc(maroon) msymbol(T)) ///
	(rspike uci_robust lci_robust yvar if fals=="11th Plan, multi hab", lstyle(ci) lw(medium) lcolor(gs10)) ///
	(scatter beta_robust yvar if fals=="11th Plan, multi hab", msize(medsmall) mlc(maroon) mfc(white) msymbol(T)) ///
	, ///
	yline(0, lcolor(black) lw(thin)) /// 
	ytitle("RD coefficient (95% CI)") xtitle("") ///
	///title("Falsificaiton test: standard errors", color(black) size(medlarge)) /// 
	graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) ///
	ylabel(-0.3 -0.2 -0.1 0 0.1 0.2 0.3 0.4,nogrid angle(0)) yscale(range(-0.125 0.4)) ///
	xlabel(2002(1)2012, labsize(medsmall)) ///
	legend(order(2 "10th Plan, single habitation  " 4 "10th Plan, multi habitation  " 6 "11th Plan, single habitaiton  " 8 "11th Plan, multi habitaiton") c(2) pos(6)) aspect(0.5)

graph export "$texfig/rd_lights_coeffs_by_year_falsif.pdf", replace 


   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_lights_by_year_falsif.tex", write text replace
 
file write tablewrite "\begin{figure}[h!]\centering" _n
file write tablewrite "\caption{Falsification test for village-level RD in nighttime brightness}" _n
file write tablewrite "\label{fig:rd_lights_by_year_falsif}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "{\includegraphics[width=0.7\textwidth, trim={1mm 10mm 1mm 5mm}, clip]{${texfig_short}/rd_lights_coeffs_by_year_falsif.pdf}} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- This figure conducts three falsification tests on our " _n
file write tablewrite "main nighttime brightness RD estimates. The \`\`falsification'' samples are identical" _n
file write tablewrite "to those in Figure \ref{fig:rd_vdelec_falsif}; see notes under Figure " _n
file write tablewrite "\ref{fig:rd_vdelec_falsif} for further detail. " _n
file write tablewrite "Whiskers display 95\% confidence intervals. " _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

** RD lights 2011 placebo test (for Figure B5)
{
use "$results/RDROBUST_fs_lights.dta", clear
keep if fs_step==1 & yvar=="lights_max2011_hat"
assert _N==1
local beta = beta_robust[1]
	
use "$sens/RDROBUST_placebo_test_lights_results.dta", clear
twoway (histogram beta_robust, freq fcolor(navy) lcolor(black)) ///
       (scatteri 0 `beta' 800 `beta', msize(0) mcolor(red) connect(l) lwidth(thick) lcolor(red)), ///  
       xscale(range(-0.03 0.03)) xlabel(-0.3 -0.2 -0.1 0 0.1 0.2 0.3, labsize(medlarge)) ///
       ytitle("Count", size(medlarge)) title("2011 nighttime brightness", size(large) color(black)) ///
			 legend(off) ylabel(,nogrid angle(0) labsize(medlarge)) xtitle("Placebo RD coefficients", size(medlarge)) ///
			 graphregion(lcolor(white)) graphregion(color(white)) plotregion(fcolor(white)) 
graph export "$texfig/placebo_test_lights_rdrobust.pdf", replace 
di `beta'
sum beta_robust, detail
_pctile beta_robust , p(91(1)99)
return list // above 99th percentile

}

********************************************************************************
********************************************************************************

** Figure B6: RD lights plots, 2002-2006 years 
{
forvalues y = 2002/2006 {
	copy "${results}/RDROBUST plots fs lights/lights_fs_preferred_`y'_wide_10.pdf" ///
		"${texfig}/lights_fs_preferred_`y'_wide_10.pdf" , replace
}

   // MAKE FIGURE
file open tablewrite using "$texfig/figure_rd_lights_plots_pre.tex", write text replace
 
file write tablewrite "\begin{figure}[h!]\centering" _n
file write tablewrite "\caption{Village-level nighttime brightness RDs by year, 2002--2006}" _n
file write tablewrite "\label{fig:rd_lights_plots_pre}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\includegraphics[width=0.465\textwidth]{${texfig_short}/lights_fs_preferred_2002_wide_10.pdf}" _n
file write tablewrite "\includegraphics[width=0.465\textwidth]{${texfig_short}/lights_fs_preferred_2003_wide_10.pdf}\\" _n
file write tablewrite "\includegraphics[width=0.465\textwidth]{${texfig_short}/lights_fs_preferred_2004_wide_10.pdf}" _n
file write tablewrite "\includegraphics[width=0.465\textwidth]{${texfig_short}/lights_fs_preferred_2005_wide_10.pdf}\\" _n
file write tablewrite "\includegraphics[width=0.465\textwidth, trim={0 5mm 0 0}, clip]{${texfig_short}/lights_fs_preferred_2006_wide_10.pdf}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- RD plots for nighttime brightness by year, " _n
file write tablewrite "which correspond to Figure \ref*{fig:rd_lights_by_year} and Table \ref{tab:rd_lights_pre}. " _n
file write tablewrite "As in Figure \ref{fig:rd_lights_plots}, we standardize the RD bandwidths and vertical axes " _n
file write tablewrite "of these brightness plots to facilitate visual comparisons across years. " _n
file write tablewrite "See notes under Figure \ref*{fig:rd_lights_by_year} for details." _n
file write tablewrite "}" _n
file write tablewrite "\end{figure}" _n

file close tablewrite


}

********************************************************************************
********************************************************************************

** Table B5: Nighttime brightness RD numerical results, 2002-2006
{
cap file close tablewrite
use "$results/RDROBUST_fs_lights.dta", clear
keep if fs_step==1
assert ifs=="pop_mismatch20==0 & lights_diff<20"
assert fe=="STFE*"
assert kernel=="Triangular"
assert polynomial==1
assert fals==""
keep if inlist(yvar,"lights_max2002_hat","lights_max2003_hat","lights_max2004_hat","lights_max2005_hat","lights_max2006_hat")

gen c = .
replace c = 1 if yvar=="lights_max2002_hat"
replace c = 2 if yvar=="lights_max2003_hat" 
replace c = 3 if yvar=="lights_max2004_hat"
replace c = 4 if yvar=="lights_max2005_hat"
replace c = 5 if yvar=="lights_max2006_hat"
assert c!=.	
sort c

forvalues c = 1/5 {
		
	local beta`c' = string(beta_robust[`c'],"%9.3f")
	local se`c' = string(se_robust[`c'],"%9.3f")
	if pval_robust[`c'] <= 0.01 {
		local stars`c' = "$^{***}$"
	}
	else if pval_robust[`c'] <= 0.05 {
		local stars`c' = "$^{**}$"
	}
	else if pval_robust[`c'] <= 0.10 {
		local stars`c' = "$^{*}$"
	}
	else {
		local stars`c' = ""
	}

	local nobs`c' = string(nobs_left[`c'] + nobs_right[`c'],"%9.0fc")
	local bw`c' = string(bw_lo[`c'], "%9.0f")
	local ymean`c' = string(ymean[`c'],"%9.3f")
}	
	
   // MAKE TABLE
file open tablewrite using "$textab/table_rd_lights_pre.tex", write text replace
 
file write tablewrite "\begin{table}[p]\centering" _n
file write tablewrite "\caption{Village-level RD in nighttime brightness, pre-RGGVY}" _n
file write tablewrite "\label{tab:rd_lights_pre}" _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\small" _n
file write tablewrite "\begin{tabular}{lccccccc} " _n
file write tablewrite "\hline " _n
file write tablewrite "& \multicolumn{5}{c}{Outcome: Maximum village brightness by year} \\ " _n
file write tablewrite "\vspace{-0.40cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "\cline{2-6} \\ " _n
file write tablewrite "\vspace{-0.80cm} " _n
file write tablewrite "\\ " _n
file write tablewrite "& 2002 & 2003 & 2004 & 2005 & 2006 \\ " _n
file write tablewrite "& (1) & (2) & (3) & (4) & (5) \\ " _n
file write tablewrite "\hline " _n
file write tablewrite "$\mathbf{1}\big[\text{2001 pop \$\ge 300\$}\big]$ & " _n
file write tablewrite "$`beta1'$`stars1' & $`beta2'$`stars2' & $`beta3'$`stars3' & $`beta4'$`stars4' & $`beta5'$`stars5' \\" _n
file write tablewrite "& $(`se1')$ & $(`se2')$ & $(`se3')$ & $(`se4')$ & $(`se5')$ \\" _n
file write tablewrite "[0.8em] " _n
file write tablewrite "Mean brightness (\$<300\$) & `ymean1' & `ymean2' & `ymean3' & `ymean4' & `ymean5' \\ " _n
file write tablewrite "Optimal bandwidth & `bw1' & `bw2' & `bw3' & `bw4' & `bw5'  \\ " _n
file write tablewrite "Village observations &`nobs1' & `nobs2' & `nobs3' & `nobs4' & `nobs5' \\ " _n
file write tablewrite "\hline" _n
file write tablewrite "\end{tabular} " _n
file write tablewrite "\vspace{-2mm}" _n
file write tablewrite "\caption*{\scriptsize Note. --- \texttt{rdrobust} specifications are identical to those in " _n
file write tablewrite "Table \ref*{tab:rd_vdelec}. " _n
file write tablewrite "Results are robust to alternative controls, kernels, bandwidth algorithms, and standard errors. " _n
file write tablewrite "See notes under Figure \ref*{fig:rd_lights_by_year} for details." _n
file write tablewrite "Significance: *** \$p < 0.01\$, ** \$p < 0.05\$, * \$p < 0.10\$." _n
file write tablewrite "}" _n
file write tablewrite "\end{table}" _n

file close tablewrite

}

********************************************************************************
********************************************************************************

